Ext.define("Admin.view.emergencyDispatch.NewPlanController", {
    extend: "Ext.app.ViewController",
    alias: "controller.newPlan",
    newPlanInfoWin: null,
    init: function() {},

    states: {
        ddjgParentId: "", //树的父级id
        ddjgSelectedTreeId: "", //treepanel选中的子叶
        ddjgTbarSelected: 'Z',
        ddjgTreeSelected: "",
        ddjgAjaxResult: ""
    },

    //下一步
    next: function() {
        var _that = this;
        var view = this.getView();
        var currentStep = view.getCurrentStep(); //获取现在的布数
        var proCd = this.lookupReference("warnProject").getValue(); //应急方案编号
        var rcd = _that.getViewModel().getData().curList.rcd; //河段编号
        var bjtjSurfaceType = this.lookupReference("bjtjSurfaceType"); //边界条件断面类型
        var bjtjPanel = this.lookupReference("bjtjPanel"); //边界条件下面的panel
        // var shuikuStartTm = this.lookupReference("shuikuStartTm"); //开始加大下泄
        // var shuikuEndTm = this.lookupReference("shuikuEndTm"); //开始加大下泄
        var newPlanChart1 = this.lookupReference("newPlanChart1").highchart; //边界条件的chartpanel
        var itemData = _that.getViewModel().getData().curList;
        switch (currentStep) {
            //第一步跳第二部
            case 0:
                if (proCd && rcd !== 0) {
                    Ext.Ajax.request({
                        url: config.URL + "emergency/edmodel/initboundry",
                        method: "post",
                        success: function(response, opts) {
                            Ext.MessageBox.hide();
                            var result = Ext.JSON.decode(response.responseText);
                            if (result.code === 0) {
                                Ext.Msg.alert("系统提示", result.msg);
                                return;
                            }
                            var listBoundry = result.listBoundry;
                            var data = [];
                            listBoundry.forEach(function(item) {
                                data.push({
                                    label: item.btype + "-" + item.bname,
                                    value: item.stcd,
                                    vtype: item.vtype,
                                    fieldnm: item.fieldnm
                                });
                            });
                            bjtjSurfaceType.setStore({
                                data: data
                            });
                            bjtjSurfaceType.setValue(data[0].value);
                            bjtjPanel.setTitle(data[0].label + data[0].vtype + "数据");
                            _that.bjtjGridChartReload(data[0].value, bjtjSurfaceType.getStore().getRange());
                            view.wnext(itemData);
                        },
                        failure: function() {
                            Ext.Msg.alert("系统提示", "系统出错！");
                        },
                        params: {
                            proCd: proCd,
                            rcd: rcd,
                            river: config.RIVER
                        }
                    });
                } else {
                    Ext.Msg.alert("系统提示", "请选择应急方案并且进行追踪溯源");
                }
                break;
                //第二步跳第三部
            case 1:
                var shuikuCombobox = this.lookupReference("shuikuCombobox"); //水库调节设置表格
                Ext.Ajax.request({
                    url: config.URL + "emergency/edmodel/initrsvr",
                    method: "post",
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 0) {
                            Ext.Msg.alert("系统提示", result.msg);
                        } else {
                            if (!result.listOTQ ||
                                !result.listrsv ||
                                result.listOTQ.length < 1 ||
                                result.listrsv.length < 1
                            ) {
                                view.wnext(itemData);
                                return;
                            }
                            _that.getViewModel().setData({
                                shuikuList: result.listrsv
                            });
                            _that.getViewModel().setData({
                                sktjData: result
                            });
                            _that.getViewModel().setData({
                                shuikuStartTm: result.listrsv[0].starttime
                            });
                            _that.getViewModel().setData({
                                shuikuEndTm: result.listrsv[0].endtime
                            });
                            // shuikuStartTm.setValue(result.listrsv[0].starttime);
                            // shuikuEndTm.setValue(result.listrsv[0].endtime);
                            shuikuCombobox.setStore({
                                data: result.listrsv
                            });
                            shuikuCombobox.setValue(result.listrsv[0].resCd);
                            _that.reloadGridChart(result.listrsv[0].resCd);
                            view.wnext(itemData);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert("系统提示", "系统出错！");
                    },
                    params: {
                        proCd: proCd,
                        rcd: rcd,
                        river: config.RIVER
                    }
                });
                break;
                //第三步跳第四步
            case 2:
                var resultTreePanel = _that.lookupReference("resultTreePanel");
                var ddjgGrid = _that.lookupReference("ddjgGrid");
                Ext.Ajax.request({
                    url: config.URL + "emergency/edmodel/initresult",
                    method: "post",
                    timeout: 100000,
                    success: function(response, opts) {
                        Ext.MessageBox.hide();
                        var result = Ext.JSON.decode(response.responseText);
                        if (result.code === 0) {
                            Ext.Msg.alert('系统提示', result.msg);
                        } else {
                            _that.states.ddjgAjaxResult = result;
                            _that.getViewModel().setData({
                                ddjgData: result
                            });
                            //设置tree的默认值
                            setTimeout(function() {
                                var record = resultTreePanel
                                    .getStore()
                                    .getNodeById(result.secTree[0].children[0].id);
                                resultTreePanel.getSelectionModel().select(record);
                            }, 1000);
                            view.wnext(itemData);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert("系统提示", "系统出错！");
                    },
                    params: {
                        proCd: proCd,
                        rcd: rcd,
                        stype: 2,
                        river: config.RIVER
                    }
                });
                break;
            default:
                view.wnext(itemData);
        }
    },

    /**
     * 调度结果 -- tree发生变化
     */
    resultTreeChange: function(me, selected, eOpts) {
        var _that = this;
        this.states.ddjgTreeSelected = selected;
        var ddjgTbarSelected = this.states.ddjgTbarSelected;
        var parentNodeId; //parentNode的ID
        var resultTbarSW = this.lookupReference('ddjgTbar-Z'); //水位
        var resultTbarRKLL = this.lookupReference('ddjgTbar-INQ'); //入库流量
        var resultTbarCKLL = this.lookupReference('ddjgTbar-OTQ'); //出库流量
        var resultTbarLL = this.lookupReference('ddjgTbar-Q'); //流量
        var resultTbarSZ = this.lookupReference('ddjgTbar-BOPL'); //水质
        var tbarList = [{
                component: resultTbarSW,
                name: 'Z',
                value: {
                    old: 'pz',
                    new: 'z'
                }
            },
            {
                component: resultTbarRKLL,
                name: 'INQ',
                value: {
                    old: 'pinq',
                    new: 'inq'
                }
            },
            {
                component: resultTbarCKLL,
                name: 'OTQ',
                value: {
                    old: 'potq',
                    new: 'otq'
                }
            },
            {
                component: resultTbarLL,
                name: 'Q',
                value: {
                    old: 'pq',
                    new: 'q'
                }
            },
            {
                component: resultTbarSZ,
                name: 'BOPL',
                value: {
                    old: 'pbopl',
                    new: 'bopl'
                }
            },
        ];
        var skList = [tbarList[0], tbarList[1], tbarList[2]];
        var otherList = [tbarList[0], tbarList[3], tbarList[4]];

        //判断树形菜单是否为子节点
        if (selected[0] && selected[0].get('leaf')) {
            parentNodeId = selected[0].parentNode.get('id'); //parentNode的ID
            this.states.ddjgSelectedTreeId = selected[0].get('id');
            //保存选中的树形节点
            this.states.ddjgParentId = parentNodeId;
            //判断是否为水库
            console.log('选中的tbar', ddjgTbarSelected);
            if (parentNodeId == 1) { //水库
                var include = false;
                otherList.forEach(function(item) {
                    item.component.setHidden(true);
                });
                skList.forEach(function(item) {
                    item.component.setHidden(false);
                    if (item.name === ddjgTbarSelected) {
                        include = true;
                    }
                });
                if (include) {
                    _that.lookupReference("ddjgTbar-" + ddjgTbarSelected).setPressed(true);
                } else {
                    skList[0].component.setPressed(true);
                    _that.states.ddjgTbarSelected = skList[0].name;
                }
                this.ddjgGridLoad();
            } else { //取水口/测站
                var include = false;
                skList.forEach(function(item) {
                    item.component.setHidden(true);
                });
                otherList.forEach(function(item) {
                    item.component.setHidden(false);
                    if (item.name === ddjgTbarSelected) {
                        include = true;
                    }
                });
                // skList[0].component.setPressed(true);
                if (include) {
                    _that.lookupReference("ddjgTbar-" + ddjgTbarSelected).setPressed(true);
                } else {
                    otherList[0].component.setPressed(true);
                    _that.states.ddjgTbarSelected = otherList[0].name;
                }
                this.ddjgGridLoad();
            }
        } else {
            console.log('父节点');
        }
    },

    // /**
    //  * 调度结果-结果数据-tbar变化
    //  */
    // ddjgSelectedTbarChange: function(me, value, oldValue, eOpts) {
    //     this.states.ddjgTbarSelected = value;
    //     this.ddjgGridLoad();
    // },

    /**
     * 调度结果-结果数据-tbar变化
     */
    ddjgSelectedTbarChange: function(me, newValue, oldValue, eOpts) {
        var _that = this;
        _that.states.ddjgTbarSelected = newValue;
        this.ddjgGridLoad();
    },

    /**
     * 调度结果-结果数据-根据参数更新表格
     */
    ddjgGridLoad: function() {
        console.log('更新了表格>>>>>>>>');
        var _that = this;
        var proCd = _that.lookupReference('warnProject').getValue(); //应急方案编号
        var rcd = _that.lookupReference('riverCd').getValue(); //应急方案编号
        var ddjgGrid = _that.lookupReference('ddjgGrid');
        var treeId = _that.states.ddjgSelectedTreeId;
        var tbarSelected = _that.states.ddjgTbarSelected;
        var list = ['Z', 'INQ', 'OTQ', 'Q', 'BOPL'];
        Ext.Ajax.request({
            url: config.URL + 'emergency/ddsEdRes/listBySecId',
            method: "post",
            timeout: 100000,
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                ddjgGrid.setStore({
                    data: result
                });
                var newPlanChart3 = _that.lookupReference('newPlanChart3').highchart; //边界条件的chartpanel
                var categoriesData = [],
                    seriesData1 = [],
                    seriesData2 = [];
                result.forEach(function(item) {
                    categoriesData.push(item.tm);
                    seriesData1.push(item.realValue);
                    seriesData2.push(item.planValue);
                });
                var step = Math.ceil(categoriesData.length / 4);
                // var index = list[tbarSelected];
                var indexName, indexUnit;
                console.log('ddjgGrid', ddjgGrid);
                switch (tbarSelected) {
                    case "Z":
                        ddjgGrid.setColumns([{
                                text: "时间",
                                align: "center",
                                dataIndex: "tm",
                                flex: 1.5,
                            },
                            {
                                text: "调度方案(m)",
                                dataIndex: "realValue",
                                flex: 1
                            },
                            {
                                text: "原计划(m)",
                                flex: 1,
                                dataIndex: "planValue"
                            }
                        ]);
                        indexName = "水位";
                        indexUnit = "m";
                        break;
                    case "INQ":
                        ddjgGrid.setColumns([{
                                text: "时间",
                                align: "center",
                                dataIndex: "tm",
                                flex: 1.5,
                            },
                            {
                                text: "调度方案(m&sup3;/s)",
                                dataIndex: "realValue",
                                flex: 1
                            },
                            {
                                text: "原计划(m&sup3;/s)",
                                flex: 1,
                                dataIndex: "planValue"
                            }
                        ]);
                        indexName = "入库流量";
                        indexUnit = "m³/s";
                        break;
                    case "OTQ":
                        ddjgGrid.setColumns([{
                                text: "时间",
                                align: "center",
                                dataIndex: "tm",
                                flex: 1.5,
                            },
                            {
                                text: "调度方案(m&sup3;/s)",
                                dataIndex: "realValue",
                                flex: 1
                            },
                            {
                                text: "原计划(m&sup3;/s)",
                                flex: 1,
                                dataIndex: "planValue"
                            }
                        ]);
                        indexName = "出库流量";
                        indexUnit = "m³/s";
                        break;
                    case "Q":
                        ddjgGrid.setColumns([{
                                text: "时间",
                                align: "center",
                                dataIndex: "tm",
                                flex: 1.5,
                            },
                            {
                                text: "调度方案(m&sup3;/s)",
                                dataIndex: "realValue",
                                flex: 1
                            },
                            {
                                text: "原计划(m&sup3;/s)",
                                flex: 1,
                                dataIndex: "planValue"
                            }
                        ]);
                        indexName = "流量";
                        indexUnit = "m³/s";
                        break;
                    case "BOPL":
                        ddjgGrid.setColumns([{
                                text: "时间",
                                align: "center",
                                dataIndex: "tm",
                                flex: 1.5,
                            },
                            {
                                text: "调度方案(mg/L)",
                                dataIndex: "realValue",
                                flex: 1
                            },
                            {
                                text: "原计划(mg/L)",
                                flex: 1,
                                dataIndex: "planValue"
                            }
                        ]);
                        indexName = "水质";
                        indexUnit = "mg/L";
                        break;
                    default:
                }
                newPlanChart3.update({
                    xAxis: {
                        categories: categoriesData,
                        labels: {
                            step: step,
                        }
                    },
                    yAxis: {
                        title: {
                            text: indexName + "(" + indexUnit + ")"
                        },
                    },
                    series: [{
                        name: '调度方案',
                        data: seriesData1
                    }, {
                        name: '原计划',
                        data: seriesData2
                    }]
                });


            },
            failure: function() {
                Ext.Msg.alert('系统提示', '系统出错！');
            },
            params: {
                proCd: proCd,
                secId: treeId,
                qType: tbarSelected,
                river: config.RIVER
            }
        });
    },

    /**
     * 调度结果-结果数据-根据参数更新曲线图
     */
    ddjgChangeChart: function(data) {},

    //水库调节水库选择change
    shuikuComboboxChange: function(that, newValue, oldValue, eOpts) {
        this.reloadGridChart(newValue);
    },

    //水库调节更新图表
    reloadGridChart: function(shuikuValue) {
        var _that = this;
        var value = shuikuValue ?
            shuikuValue :
            _that.getViewModel().get("shuikuList").resCd;
        var proCd = this.lookupReference("warnProject").getValue(); //应急方案编号
        var rcd = _that.getViewModel().getData().curList.rcd; //河段编号
        Ext.Ajax.request({
            url: config.URL + "emergency/edmodel/initrsvr",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                if (result.code === 0) {
                    Ext.Msg.alert("系统提示", result.msg);
                } else {
                    if (!result.listOTQ ||
                        !result.listrsv ||
                        result.listOTQ.length < 1 ||
                        result.listrsv.length < 1
                    ) {
                        view.wnext(itemData);
                        return;
                    }
                    _that.getViewModel().setData({
                        shuikuList: result.listrsv
                    });
                    _that.getViewModel().setData({
                        sktjData: result
                    });

                    // var result = _that.getViewModel().getData().sktjData;
                    var ddsDrPptnGrid = _that.lookupReference("ddsDrPptnGrid");
                    var newPlanChart2 = _that.lookupReference("newPlanChart2").highchart; //边界条件的chartpanel
                    var categoriesData = [],
                        seriesData1 = [],
                        seriesData2 = [];
                    var list = result.listOTQ[value] ? result.listOTQ[value] : [];
                    ddsDrPptnGrid.setStore({
                        data: list
                    });
                    result.listrsv.forEach(function(item) {
                        if (item.resCd == value) {
                            item.starttime = new Date(item.starttime);
                            item.endtime = new Date(item.endtime);
                            _that.getViewModel().setData({
                                shuikuList: item
                            });
                        }
                    });
                    //生成chart的数据
                    list.forEach(function(item) {
                        categoriesData.push(item.tm);
                        seriesData1.push(item.otq);
                        seriesData2.push(item.exq);
                    });
                    var step = Math.ceil(categoriesData.length / 4);
                    newPlanChart2.update({
                        xAxis: {
                            categories: categoriesData,
                            labels: {
                                step: step
                            }
                        },
                        series: [{
                                data: seriesData1
                            },
                            {
                                data: seriesData2
                            }
                        ]
                    });
                }

            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错！");
            },
            params: {
                proCd: proCd,
                rcd: rcd,
                river: config.RIVER
            }
        });

    },

    //水库调节-水质调度目标-详细设置的弹出框
    wqObjectiveHandler: function() {
        var _that = this;
        var result = _that.getViewModel().get("sktjData.listconsec");
        var win = _that.lookupReference("wqObjectiveWindow");
        if (!win) {
            win = Ext.create("Ext.window.Window", {
                title: "水质调度目标设置",
                width: 500,
                //      modal: true,
                reference: "wqObjectiveWindow",
                autoScroll: true,
                // buttons: [
                //   {
                //     text: "取消",
                //     handler: function() {
                //       var modal = this.up("window");
                //       modal.hide();
                //     }
                //   },
                //   {
                //     text: "保存"
                //   }
                // ],
                items: {
                    xtype: "wqObjective",
                    layout: {
                        type: "vbox",
                        pack: "start",
                        align: "stretch"
                    }
                }
            });
            this.getView().add(win);
        }
        win.show();
    },

    /**
     * 水库调节-增加下泄流量导入数据
     * @return {[type]}          [description]
     */
    addFlowUpdateFile: function(me, newValue, oldValue, eOpts) {
        var _that = this;
        var form = me.up("form").getForm();
        var values = form.getValues();
        var Range = _that.lookupReference('shuikuCombobox').getValue();

        if (form.isValid()) {
            form.submit({
                url: config.URL + "emergency/ddsEdRsv/importdata",
                waitMsg: "正在上传文件...",
                success: function(fp, o) {
                    // _that.sktjReload();
                    _that.reloadGridChart(Range);
                    Ext.Msg.alert("成功", '"你的文件"' + o.result.file + '"已经上传成功"');
                },
                failure: function(fp, o) {
                    // console.log(361, fp, o);
                    _that.reloadGridChart(Range);
                    Ext.Msg.alert('失败', '上传失败!');

                },
            });
        }
    },

    /**
     * 水库调节-原始下泄流量导入数据
     * @return {[type]}          [description]
     */
    oldFlowUpdateFile: function(me, newValue, oldValue, eOpts) {
        var _that = this;
        if (!newValue) { return; }
        var form = me.up("form").getForm();
        var values = form.getValues();
        var Range = _that.lookupReference('shuikuCombobox').getValue();
        // console.log(Range);
        if (form.isValid()) {
            form.submit({
                url: config.URL + "emergency/ddsEdRsv/importdata",
                waitMsg: "正在上传文件...",
                success: function(fp, o) {
                    // _that.sktjReload();
                    _that.reloadGridChart(Range);
                    Ext.Msg.alert("成功", '"你的文件"' + o.result.file + '"已经上传成功"');
                },
                failure: function(fp, o) {
                    // console.log('Range', Range);
                    _that.reloadGridChart(Range);
                    // console.log(361, fp, o);
                    // Ext.Msg.alert('失败', '上传失败!');

                },
            });
        }
    },

    sktjObjectiveSet: function(editor, context, eOpts) {
        var _that = this;
        if (!newValue) { return; }
        var newValue = context.value;
        var data = context.record.getData();
        var wqObjectiveGrid = _that.lookupReference("wqObjectiveGrid");
        Ext.Ajax.request({
            url: config.URL + "emergency/ddsEdInit/updateConcstById",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                if (result.code === 1) {
                    wqObjectiveGrid.getStore().reload();
                    Ext.Msg.alert("系统提示", "修改成功!");
                } else {
                    Ext.Msg.alert("系统提示", result.msg);
                }
            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错！");
            },
            params: {
                proCd: data.procd,
                secid: data.secid,
                concSt: data.concst,
                river: config.RIVER
            }
        });
    },

    /**
     * 水库调节-ajax请求最新数据更新viewModel的sktjDate
     * @return {[type]} [description]
     */
    sktjReload: function() {
        var _that = this;
        var rcd = _that.getViewModel().getData().curList.rcd; //河段编号
        var proCd = _that.getViewModel().getData().curList.proCd; //应急方案编号
        var resCd = _that.lookupReference('shuikuCombobox').getValue();
        Ext.Ajax.request({
            url: config.URL + "emergency/edmodel/initrsvr",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                _that.getViewModel().setData({
                    sktjData: result
                });
                _that.reloadGridChart(resCd);
            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错！");
            },
            params: {
                proCd: proCd,
                rcd: rcd,
                river: config.RIVER
            }
        });
    },

    /**
     * 水库调节-原计划流量读取
     * @return {[type]} [description]
     */
    fetchOriginalPlan: function() {
        var _that = this;
        var rcd = _that.getViewModel().getData().curList.rcd; //河段编号
        var proCd = _that.getViewModel().getData().curList.proCd; //应急方案编号
        Ext.Ajax.request({
            url: config.URL + "emergency/edmodel/initrsvr",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                _that.getViewModel().setData({
                    sktjData: result
                });
                _that.reloadGridChart();
                Ext.Msg.alert("系统提示", "读取原计划下泄成功!");
            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错！");
            },
            params: {
                proCd: proCd,
                rcd: rcd,
                otqtype: 1,
                river: config.RIVER
            }
        });
    },

    /**
     * 水库调节-加大下泄流量固定值
     * @return {[type]} [description]
     */
    addFixedFlow: function() {
        var _that = this;
        var win = _that.lookupReference("addFlowWindow");
        if (!win) {
            win = Ext.create("Ext.window.Window", {
                title: "设置加大下泄流量固定值",
                width: 530,
                layout: "fit",
                autoScroll: true,
                reference: "addFlowWindow",
                itemId: "addFlowWindow",
                buttons: [{
                        text: "取消",
                        handler: function() {
                            var modal = this.up("window");
                            modal.hide();
                        }
                    },
                    {
                        text: "保存",
                        handler: function() {
                            var form = _that.lookupReference("addFlowForm").getForm();
                            var modal = this.up("window");
                            var exq = form.getValues().exq;
                            var proCd = _that.getViewModel().getData().curList.proCd;
                            var rcd = _that.getViewModel().getData().curList.rcd;
                            var resCd = _that.getViewModel().getData().shuikuList.resCd;
                            var shuikuStartTm = form.getValues().shuikuStartTm;
                            var shuikuEndTm = form.getValues().shuikuEndTm;
                            // _that.getViewModel().setData({
                            //     shuikuStartTm: result.listrsv[0].starttime
                            // });
                            // _that.getViewModel().setData({
                            //     shuikuEndTm: result.listrsv[0].endtime
                            // });
                            Ext.Ajax.request({
                                url: config.URL + "emergency/edmodel/updateRsvrType",
                                method: "post",
                                success: function(response, opts) {
                                    var result = Ext.JSON.decode(response.responseText);
                                    if (result.code === 0) {
                                        Ext.Msg.alert("系统提示", result.msg);
                                    } else {
                                        _that.getViewModel().setData({
                                            shuikuStartTm: shuikuStartTm
                                        });
                                        _that.getViewModel().setData({
                                            shuikuEndTm: shuikuEndTm
                                        });
                                        // _that.getViewModel().setData({ sktjData: result });
                                        // _that.getViewModel().setData({ addFlowDate: exq });
                                        // _that.reloadGridChart();
                                        _that.sktjReload();
                                        modal.hide();
                                        // Ext.Msg.alert("系统提示", "加大下泄固定值修改成功");
                                    }
                                },
                                failure: function() {
                                    Ext.Msg.alert("系统提示", "系统出错！");
                                },
                                params: {
                                    exq: exq,
                                    proCd: proCd,
                                    resCd: resCd,
                                    rcd: rcd,
                                    river: config.RIVER
                                }
                            });
                        }
                    }
                ],
                items: {
                    xtype: "baseFormPanel",
                    border: false,
                    reference: "addFlowForm",
                    layout: "form",
                    items: [{
                            xtype: "textfield",
                            labelWidth: 90,
                            fieldLabel: "加大下泄流量固定值",
                            name: "exq"
                        },
                        {
                            xtype: "timesfield",
                            fieldLabel: "开始加大下泄",
                            labelWidth: 90,
                            name: "shuikuStartTm",
                            reference: "shuikuStartTm",
                            format: 'Y-m-d H:i:s',
                            // bind: {
                            //     value: "{shuikuStartTm}"
                            // },
                            listeners: {
                                change: function(me, newValue, oldValue, eOpt) {
                                    _that.sktjStartTimeChange(me, newValue, oldValue, eOpt);
                                }
                            }
                        },
                        {
                            xtype: "timesfield",
                            labelWidth: 90,
                            fieldLabel: "结束加大下泄",
                            name: "shuikuEndTm",
                            reference: "shuikuEndTm",
                            format: 'Y-m-d H:i:s',
                            // bind: {
                            //     value: "{shuikuEndTm}"
                            // },
                            listeners: {
                                change: function(me, newValue, oldValue, eOpt) {
                                    _that.sktjEndTiemChange(me, newValue, oldValue, eOpt);
                                }
                            }
                        }
                    ]
                }
            });
            this.getView().add(win);
        }
        win.show();
    },

    /**
     * 水库调节-开始时间变化监听
     * @param  {Object} me       datefield组件自身
     * @param  {Date} newValue   修改日期
     * @param  {Date} oldValue   原始值
     * @param  {[type]} eOpt     [description]
     * @return {[type]}          [description]
     */
    sktjStartTimeChange: function(me, newValue, oldValue, eOpt) {
        if (!oldValue) return;
        var _that = this;
        var rcd = _that.getViewModel().getData().curList.rcd; //河段编号
        var proCd = _that.getViewModel().getData().curList.proCd; //应急方案编号
        var shuikuList = _that.getViewModel().getData().shuikuList;
        Ext.Ajax.request({
            url: config.URL + "emergency/ddsMRsvr/updateTimeById",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                if (result.code === 1) {
                    _that.sktjReload();
                    shuikuList.starttime = newValue;
                    _that.getViewModel().setData({
                        shuikuList: shuikuList
                    });
                } else {
                    Ext.Msg.alert("系统提示", result.msg);
                }
            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错！");
            },
            params: {
                proCd: proCd,
                rcd: rcd,
                starttime: Ext.Date.parse(newValue, "Y-m-d H:i:s"),
                river: config.RIVER
            }
        });
    },

    /**
     * 水库调节-结束时间变化监听
     * @param  {Object} me       datefield组件自身
     * @param  {Date} newValue   修改日期
     * @param  {Date} oldValue   原始值
     * @param  {[type]} eOpt     [description]
     * @return {[type]}          [description]
     */
    sktjEndTiemChange: function(me, newValue, oldValue, eOpt) {
        if (!oldValue) return;
        var _that = this;
        var rcd = _that.getViewModel().getData().curList.rcd; //河段编号
        var proCd = _that.getViewModel().getData().curList.proCd; //应急方案编号
        var shuikuList = _that.getViewModel().getData().shuikuList;
        Ext.Ajax.request({
            url: config.URL + "emergency/ddsMRsvr/updateTimeById",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                if (result.code === 1) {
                    _that.sktjReload();
                    shuikuList.starttime = newValue;
                    _that.getViewModel().setData({
                        shuikuList: shuikuList
                    });
                } else {
                    Ext.Msg.alert("系统提示", result.msg);
                }
            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错！");
            },
            params: {
                proCd: proCd,
                rcd: rcd,
                starttime: Ext.Date.parse(newValue, "Y-m-d H:i:s"),
                river: config.RIVER
            }
        });
    },

    /**
     * 水库调节-保存
     * @return {[type]} [description]
     */
    sktjSave: function() {
        var _that = this;
        var rcd = _that.getViewModel().getData().curList.rcd; //河段编号
        var proCd = _that.getViewModel().getData().curList.proCd; //应急方案编号
        var resCd = _that.lookupReference('shuikuCombobox').getValue();
        // var starttime = _that.lookupReference('shuikuStartTm').getValue();
        // var endtime = _that.lookupReference('shuikuEndTm').getValue();
        var starttime = _that.getViewModel().getData().shuikuStartTm;
        var endtime = _that.getViewModel().getData().shuikuEndTm;
        // _that.getViewModel().setData({
        //     shuikuStartTm: result.listrsv[0].starttime
        // });
        // _that.getViewModel().setData({
        //     shuikuEndTm: result.listrsv[0].endtime
        // });
        var shuikuList = _that.getViewModel().getData().shuikuList;
        var z = _that.lookupReference("shuikuCssw").getValue();
        Ext.Ajax.request({
            url: config.URL + "emergency/ddsMRsvr/updateTimeById",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                if (result.code === 1) {
                    Ext.Msg.alert("系统提示", "保存成功!");
                } else {
                    Ext.Msg.alert("系统提示", result.msg);
                }
            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错！");
            },
            params: {
                proCd: proCd,
                rcd: rcd,
                resCd: resCd,
                starttime: Ext.Date.parse(starttime, "Y-m-d H:i:s"),
                endtime: Ext.Date.parse(endtime, "Y-m-d H:i:s"),
                z: z
            }
        });
    },

    /**
     * 边界条件-下载
     */
    bjtjDownload: function() {
        var _that = this;
        var proCd = _that.getViewModel().getData().curList.proCd; //应急方案编号
        var stcd = _that.lookupReference('bjtjSurfaceType').getValue(); //
        window.open(
            config.URL + "emergency/ddsEdBound/downtempletfile?river=" + config.RIVER + "&proCd=" + proCd + "&stcd=" + stcd
        );
    },
    bjtjDownload31: function() {
        var _that = this;
        var proCd = _that.getViewModel().getData().curList.proCd; //应急方案编号
        var stcd = _that.lookupReference('bjtjSurfaceType').getValue(); //
        window.open(
            config.URL + "emergency/ddsEdRsv/downtempletfile?river=" + config.RIVER + "&proCd=" + proCd + "&stcd=" + stcd
        );
    },
    /**
     * 边界条件导入
     * @return {[type]} [description]
     */
    bjtjUpdateFile: function(field, value) {
        console.log("field", field);
        console.log("value", value);
    },

    //边界条件断面change
    bjtjSurfaceTypeChange: function(that, newValue, oldValue, eOpts) {
        var range = that.getStore().getRange();
        this.bjtjGridChartReload(newValue, range);
    },

    //边界条件切换断面重载
    bjtjGridChartReload: function(newValue, range) {
        var that = this;
        this.getViewModel().setData({
            stcd: newValue
        });
        var proCd = this.lookupReference("warnProject").getValue(); //应急方案编号
        var bjtjPanel = this.lookupReference("bjtjPanel"); //边界条件下面的panel
        var llsjGrid = this.lookupReference("llsjGrid"); //边界条件下面的grid
        var newPlanChart1 = this.lookupReference("newPlanChart1").highchart; //边界条件的chartpanel
        var llsjGridStoreRange = llsjGrid.getStore().getRange();
        var typeIndex; //当前指标标识符
        var seriesData = [],
            categoriesData = [],
            seriesName;
        //設置title
        range.forEach(function(item) {
            if (item.data.value === newValue) {
                // console.log("item", item);
                bjtjPanel.setTitle(item.data.label + item.data.vtype + "数据");
                typeIndex = item.data.fieldnm.toLowerCase();
                seriesName = item.data.vtype;
                llsjGrid.setColumns([{
                        text: "时间",
                        dataIndex: "tm",
                        flex: 1.2,
                        align: "center"
                    },
                    {
                        text: item.data.vtype === "水位" ? item.data.vtype + '(m)' : item.data.vtype + "(m³/s)",
                        dataIndex: item.data.fieldnm.toLowerCase(),
                        flex: 1,
                        align: "center",
                        editor: {
                            field: {
                                xtype: 'numberfield',
                                allowBlank: false
                            }
                        },
                        renderer: function(value, record) {
                            if (!value) return "";
                            return '<span style="color: #35baf6">' + value + '</span>';
                        }
                    }
                ]);
            }
        });
        Ext.Ajax.request({
            url: config.URL + "emergency/ddsEdBound/list",
            method: "post",
            success: function(response, opts) {
                Ext.MessageBox.hide();
                var result = Ext.JSON.decode(response.responseText);
                var list = result.list && result.list.length > 0 ? result.list : [];
                llsjGrid.setStore({
                    data: list
                });
                list.forEach(function(item) {
                    seriesData.push(item[typeIndex]);
                    categoriesData.push(item.tm);
                });
                var step = Math.ceil(categoriesData.length / 4);
                newPlanChart1.update({
                    yAxis: {
                        title: {
                            text: seriesName + "(" + (seriesName === "水位" ? "m" : "m³/s") + ")"
                        }
                    },
                    xAxis: {
                        categories: categoriesData,
                        labels: {
                            step: step
                        }
                    },
                    series: [{
                        name: seriesName,
                        data: seriesData
                    }]
                });
            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错！");
            },
            params: {
                proCd: proCd,
                stcd: newValue,
                river: config.RIVER
            }
        });
    },

    //根据应急事件设置事件信息
    setEventData: function(that, newValue, oldValue, eOpts) {
        var params = {
            evenCd: newValue,
            river: config.RIVER
        };
        var refs = this.getReferences();
        Ext.Ajax.request({
            url: config.URL + "emergency/ddsEdEvent/get",
            method: "post",
            success: function(response, opts) {
                var result = Ext.util.JSON.decode(response.responseText);
                if (response) {
                    //污染物类型
                    var WRWLX = refs.WARN_FIELD_WRWLX;
                    WRWLX.setValue("请选择物质类型"); //response.daType
                    //物质初始浓度
                    var WZCSND = refs.WARN_FIELD_WZCSND;
                    WZCSND.setValue(result.da); //result.daType
                    //发生河段
                    var FSHD = refs.WARN_FIELD_FSHD;
                    FSHD.setValue(result.rcd); //result.daType
                    //发生地点
                    var FSDD = refs.WARN_FIELD_FSDD;
                    FSDD.setValue(result.loc); //result.daType
                    //参考断面
                    var CKDM = refs.WARN_FIELD_CKDM;
                    CKDM.setValue(result.downsec); //result.daType
                    //上断面距离
                    var SDMJL = refs.WARN_FIELD_SDMJL;
                    SDMJL.setValue(result.upsec); //result.daType
                    //污染发生时间
                    var WRFSSJ = refs.WARN_FIELD_WRFSSJ;
                    WRFSSJ.setValue(result.hapDt); //result.daType
                    //持续时间
                    var CXSJ = refs.WARN_FIELD_CXSJ;
                    CXSJ.setValue(result.dur); //result.daType
                    //起始时间
                    var QSSJ = refs.WARN_FIELD_QSSJ;
                    QSSJ.setValue(result.pubDt); //result.daType
                    //终止时间
                    var ZZSJ = refs.WARN_FIELD_ZZSJ;
                    ZZSJ.setValue(result.edt); //result.daType
                    //时间尺度
                    var SJCD = refs.WARN_FIELD_SJCD;
                    SJCD.setValue(result.tscale); //result.daType
                    //预热期
                    var YRQ = refs.WARN_FIELD_YRQ;
                    YRQ.setValue(""); //result.daType
                } else {
                    Ext.Msg.alert("系统提示", "获取数据失败");
                    modal.close();
                }
            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错！");
            },
            params: params
        });
    },

    //第一步选择方案
    wanrProjectChange: function(me, newVlaue, oldValue, eOpts) {
        var list = me.getStore().getRange();
        var _that = this;
        list.forEach(function(item, index) {
            var itemData = item.getData();
            if (itemData.proCd === newVlaue) {
                itemData.daType = getValue("datype", itemData.daType);
                me.curList = itemData;

                _that.getViewModel().setData({
                    curList: itemData
                });
            }
        });

    },

    //第二步边界条件-断面类型初始化
    bjtjSurfaceTypeAdded: function(me) {
        var _that = this;
    },

    //模型参数设置controller
    onParamSetClick: function() {
        var _that = this;
        var win = _that.lookupReference("modelAddWindow");
        if (!win) {
            win = Ext.create("Ext.window.Window", {
                title: "模型参数设置",
                width: 350,
                layout: "fit",
                closeAction: "hide",
                resizable: false,
                autoScroll: true,
                reference: "modelAddWindow",
                itemId: "modelAddWindow",
                buttons: [{
                        text: "保存",
                        handler: function() {
                            var form = _that.lookupReference("modelAddForm").getForm();
                            if (form.isValid()) {
                                var modal = this.up("window");
                                var values = form.getValues();
                                var proCd = _that.lookupReference("warnProject").getValue(); //应急方案编号
                                values.proCd = proCd;
                                values.river = config.RIVER;
                                Ext.Ajax.request({
                                    url: config.URL + "/emergency/ddsEdMedel/save",
                                    method: "post",
                                    params: values,
                                    success: function(response, opts) {
                                        var result = Ext.JSON.decode(response.responseText);
                                        Ext.MessageBox.hide();
                                        if (result.code === 1) {
                                            Ext.Msg.alert("系统提示", "保存成功");
                                            modal.hide();
                                        } else {
                                            Ext.Msg.alert("系统提示", result.msg);
                                        }
                                    },
                                    failure: function() {
                                        Ext.Msg.alert("系统提示", "系统出错！");
                                    }
                                });
                            } else {
                                Ext.Msg.alert("提示", "表单填写不合法！");
                            }
                        }
                    },
                    {
                        text: "取消",
                        handler: function() {
                            var modal = this.up("window");
                            modal.hide();
                        }
                    },

                ],
                items: {
                    xtype: "modelForm",
                    border: false,
                    reference: "modelAddForm"
                }
            });
            this.getView().add(win);
            // Ext.ComponentQuery.query("#mainView")[0].add(win);
        }
        //更新模型参数
        var proCd = _that.lookupReference("warnProject").getValue();
        Ext.Ajax.request({
            url: config.URL + "emergency/ddsEdMedel/get",
            method: "post",
            params: {
                proCd: proCd,
                river: config.RIVER
            },
            success: function(response, opts) {
                var result = Ext.JSON.decode(response.responseText);
                if (!result.a) {
                    _that.lookupReference('modelAddForm').getForm().setValues({
                        a: "",
                        r: "",
                        k: ""
                    });
                } else {
                    _that.lookupReference('modelAddForm').getForm().setValues(result);
                }
            },
            failure: function() {
                Ext.Msg.alert("系统提示", "系统出错! ");
            }
        });
        win.show();
    },
    onNewPlanInfoClick: function(e) {
        var _that = this;
        var x = e.getX();
        var y = e.getY();
        var iconCls = e.iconCls;
        if (iconCls.indexOf("fa-chevron-left") != -1) {
            e.setIconCls("x-fa fa-chevron-right");
            this.newPlanInfoWin = Ext.create("Ext.window.Window", {
                title: "方案基本信息",
                height: 520,
                width: 310,
                resizable: false,
                closeAction: "hide",
                closable: false,
                layout: "fit",
                items: {
                    xtype: "newPlanInfo",
                    scrollable: true,
                    reference: "bjtjSchemeInfor",
                    border: false
                }
            }).showAt(x - 315, y);
            _that.getView().add(this.newPlanInfoWin);
            var curList = _that.getViewModel().get("curList");
            curList.loc1 = curList.loc;
            mriverList.forEach(function(item) {
                if (item.rcd == curList.rcd) {
                    curList.rnm = item.rnm;
                }
            });
            _that
                .lookupReference("bjtjSchemeInfor")
                .getForm()
                .setValues(curList);
        } else {
            e.setIconCls("x-fa fa-chevron-left");
            this.newPlanInfoWin.close();
        }
    },
    jdxxfsChange: function(obj) {
        // console.log(obj.getValue());
    },
    supermapPlay: function() {
        var elem = $("#supermapDemo")[0];
        var doc = elem.contentWindow;
        doc.startAnimator();
    },
    supermapPause: function() {
        var elem = $("#supermapDemo")[0];
        var doc = elem || elem.contentWindow;
        doc.pauseAnimator();
    },

    //断面设置表格点击
    duanmianGridClick: function() {
        // console.log("sadfasdfasdf");
    },



    //第二步单元格修改
    llsjGridEdit: function(editor, context, eOpts) {
        var that = this;

        // console.log(value);
        var bjtjSurfaceType = that.lookupReference("bjtjSurfaceType"); //边界条件断面类型
        var newValue = bjtjSurfaceType.getValue();
        var range = bjtjSurfaceType.getStore().getRange();
        // console.log(range);
        // console.log(context.field);
        // var typeIndex = '';
        var typeIndex = context.field;
        var newPlanChart1 = that.lookupReference("newPlanChart1").highchart; //边界条件的chartpanel

        setTimeout(function() {
            var llsjGrid = that.lookupReference("llsjGrid"); //边界条件下面的grid
            var llsjstore = llsjGrid.getStore();
            var value = Ext.pluck(context.store.data.items, 'data');
            var changeData = value[context.rowIdx];
            var state; //返回的狀態嘛
            Ext.Ajax.request({
                url: config.URL + "emergency/ddsEdBound/update",
                method: "post",
                success: function(response, opts) {
                    Ext.MessageBox.hide();
                    var result = Ext.JSON.decode(response.responseText);
                    if (result.code === 1) {
                        state = true;
                        Ext.toast({
                            html: result.msg,
                            width: 200,
                            align: 't'
                        });
                        llsjstore.load({
                            scope: this,
                            callback: function(records, operation, success) {
                                var data = llsjstore.getData().items;
                                var seriesData = [],
                                    categoriesData = [],
                                    seriesName;

                                data.forEach(function(item) {
                                    // console.log(item);
                                    seriesData.push(item.data[typeIndex]);
                                    categoriesData.push(item.data.tm);
                                });
                                var step = Math.ceil(categoriesData.length / 4);

                                newPlanChart1.update({
                                    xAxis: {
                                        categories: categoriesData,
                                        labels: {
                                            step: step
                                        }
                                    },
                                    series: [{
                                        name: seriesName,
                                        data: seriesData
                                    }]
                                });
                            }
                        });

                    } else {
                        state = false;
                        llsjstore.reload();
                        Ext.toast({
                            html: result.msg,
                            width: 200,
                            align: 't'
                        });
                    }
                },
                failure: function() {
                    state = false;
                    llsjstore.reload();
                    Ext.Msg.alert("系统提示", "系统出错！");
                },
                params: changeData
            });
            return state;
        }, 10);
    },

    //第三步单元格修改
    ddsDrPptnGridEdit: function(editor, context, eOpts) {
        var that = this;

        // console.log(value);
        // var bjtjSurfaceType = that.lookupReference("bjtjSurfaceType"); //边界条件断面类型
        // var newValue = bjtjSurfaceType.getValue();
        // var range = bjtjSurfaceType.getStore().getRange();
        // console.log(range);
        // console.log(context.field);
        // var typeIndex = '';
        var typeIndex = context.field;
        var newPlanChart2 = that.lookupReference("newPlanChart2").highchart; //边界条件的chartpanel

        setTimeout(function() {
            var ddsDrPptnGrid = that.lookupReference("ddsDrPptnGrid"); //边界条件下面的grid
            var llsjstore = ddsDrPptnGrid.getStore();
            var value = Ext.pluck(context.store.data.items, 'data');
            var changeData = value[context.rowIdx];
            changeData.otqtype = 1;
            changeData.exqtype = 1;
            // console.log(changeData);
            var state; //返回的狀態嘛
            Ext.Ajax.request({
                url: config.URL + "emergency/ddsEdRsv/update",
                method: "post",
                success: function(response, opts) {
                    Ext.MessageBox.hide();
                    var result = Ext.JSON.decode(response.responseText);
                    if (result.code === 1) {
                        state = true;
                        Ext.toast({
                            html: result.msg,
                            width: 200,
                            align: 't'
                        });
                        // llsjstore.reload();
                        llsjstore.load({
                            scope: this,
                            callback: function(records, operation, success) {
                                var data = llsjstore.getData().items;
                                console.log(data);

                                var seriesData = [],
                                    categoriesData1 = [],
                                    categoriesData2 = [];

                                data.forEach(function(item) {
                                    categoriesData1.push(item.data.otq);
                                    categoriesData2.push(item.data.exq);
                                    seriesData.push(item.data.tm);
                                });
                                var step = Math.ceil(seriesData.length / 4);

                                newPlanChart2.update({
                                    xAxis: {
                                        categories: seriesData,
                                        labels: {
                                            step: step
                                        }
                                    },
                                    series: [{
                                            name: "原计划流量",
                                            data: categoriesData1
                                        },
                                        {
                                            name: "加大流量",
                                            data: categoriesData2
                                        }
                                    ]
                                });
                            }
                        });

                    } else {
                        state = false;
                        llsjstore.reload();
                        Ext.toast({
                            html: result.msg,
                            width: 200,
                            align: 't'
                        });
                    }
                },
                failure: function() {
                    state = false;
                    llsjstore.reload();
                    Ext.Msg.alert("系统提示", "系统出错！");
                },
                params: changeData
            });
            return state;
        }, 10);
    },
    uploadDatatwo: function() {
        var that = this;
        var form = that.lookupReference('uploadDatatwo').getForm();

        var values = form.getValues();
        var Range = that.lookupReference('bjtjSurfaceType').getStore().getRange();
        // console.log(Range);
        // console.log(values);
        if (form.isValid()) {
            form.submit({
                url: config.URL + "emergency/ddsEdBound/importdata",
                waitMsg: "正在上传文件...",
                success: function(fp, o) {
                    // console.log(354, fp, o);
                    Ext.Msg.alert(
                        "成功",
                        '"你的文件"' + o.result.file + '"已经上传成功"'
                    );
                    that.bjtjGridChartReload(values.stcd, Range);
                },
                failure: function(fp, o) {
                    // console.log(361, fp, o);
                    Ext.Msg.alert('失败', '上传失败!');
                    that.bjtjGridChartReload(values.stcd, Range);

                },
            });
        }
    }
});